<?php

namespace App\Admin\Controllers;

use App\Program;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use App\Type;

class ProgramsController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('首页')
            ->description('列表')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('查看')
            ->description('详细信息')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('修改')
            ->description('修改信息')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('创建')
            ->description('创建游戏')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Program());
        $grid->filter(function ($filter) {

            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            // 在这里添加字段过滤器
            $filter->like('name', '游戏');
        });

        $grid->column('id', '序号')->sortable();
        $grid->name('游戏名称');
        $grid->column('type.name', '分类名称');
        $grid->versionnumber('版本号');
        $grid->program_file('program_file', '文件下载地址');
        $grid->download('下载次数');
//        $grid->column('file_size','文件大小(M)')->display(function () {
//           if ($this->program_file != null) {
//               $url = "http://app.git51.cn/storage/" . $this->program_file;
//               $res = get_headers($url,true);
//              return $file_size = round($res['Content-Length']/1024/1024,2);
//           }
//        });
        $grid->versionCode('版本号');
        $grid->logo('logo图');
        $grid->apk('项目名');
        $grid->imgs('产品截图');
        $grid->updated_at('更新时间');

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Program::findOrFail($id));

        $show->id('序号');
        $show->name('游戏名称');
        $show->type_id('分类');
        $show->versionnumber('版本号');
        $show->program_file('文件下载地址');
        $show->created_at('创建时间');
        $show->updated_at('更新时间');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Program());

        $form->text('name', '游戏名称');
        $form->select('type_id', '游戏分类')->options(Type::all()->pluck('name', 'id'));
        $form->text('versionnumber', '版本号');
        $form->display('program_file', '当前文件地址');
        $form->largefile('Program_file', '上传文件');
        $form->number('download', '下载数量');
        $form->number('versionCode', '版本号');
        $form->text('apk', '项目名');
        $form->image('logo', 'logo图');
        $form->textarea('description', '产品描述');
        $form->multipleImage('imgs', '产品截图')->removable();
        return $form;
    }
}
